// pages/order/orderPage/index.js
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {},
    list: []
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 读取菜品信息
    let order = wx.getStorageSync('goOrder');
    if (order) {
      this.setOrder(order);
      this.getList();
    }
  },

  // 支付
  goPay() {
    // 余额判断

    if (this.data.order.user.balance < this.data.order.total) {
      console.log('跳页')
      wx.redirectTo({url:'/pages/recharge/index'})
      return false;
    }
    let url = 'mp/order/pay/' + this.data.order.id;
    App.ajax(url, 'POST', '').then(res => {
      if (res.status === 10000) {
        wx.redirectTo({url:'/pages/order/index'})
      } else {
        App.prompts(res.msg)
      }
    }).catch(err => {
      console.log(err)
    })
  },
  // 生成订单
  setOrder(param) {
    let url  = 'mp/order';
    App.ajax(url, 'POST', param).then(res => {
      if (res.status === 10000) {
        this.setData({
          order: res.data
        })
      } else {
        App.prompts(res.msg)
      }
    }).catch(err => {
      console.log(err)
    })
  },
  // 猜你喜欢
  getList() {
    let url = 'mp/dishes/like';
    App.ajax(url, 'GET', '').then(res => {
      console.log(res);
      if (res.status === 10000) {
        this.setData({
          list: res.data
        })
      }
    }).catch(err => {
      console.log(err)
    })
  }
})